package com.xyy.saas.payment.merchant.core.manager;

import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.dao.mapper.PaymentAppMapper;
import com.xyy.saas.payment.dao.model.PaymentApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wangtianqi
 * @date 2019/12/5 13:40
 */
@Component
public class PaymentAppManager {

    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(PaymentAppManager.class);

    private static ConcurrentHashMap<String, PaymentApp> paymentAppMap = new ConcurrentHashMap<>();

    @Autowired
    PaymentAppMapper paymentAppMapper;

    public String getChannelApp(String businessType) throws PaymentException {
        PaymentApp paymentApp = paymentAppMap.get(businessType);
        if(paymentApp == null) {
            paymentApp = paymentAppMapper.selectByBusinessType(businessType, Constants.YIZHANGTONG);
            if(paymentApp == null) {
                LOGGER.warn("PaymentAppManager#getChannelApp invoker is unavailable businessType:{}", businessType);
                throw new PaymentException(ResultCodeEnum.APP_UNAVAILABLE);
            }
            paymentAppMap.putIfAbsent(businessType, paymentApp);
        }
        return paymentApp.getChannelApp();
    }

}
